/* drawing-tools.vala
 *
 * Copyright (C) 2010  Richard Talbot-Watkins
 * Copyright (C) 2010  Dave Jeffery
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Authors:
 *  Richard Talbot-Watkins <richtw1@gmail.com>
 * 	Dave Jeffery <david.richard.jeffery@gmail.com>
 */
 
using Gtk;
 
/**
 * Toolbox window. This window is a GTK utility window, so it has a small
 * title bar and can't be minimised/maximised. Stock GTK icon is used
 * for paste to comply with GNOME HIG guidelines, whereas the other five
 * icons are loaded from pngs.
 *
 * The buttons are public so the parent window can link other widgets, 
 * such as a set of menu bar radio buttons, to their state.
 *
 * @author Dave Jeffery <david.richard.jeffery@gmail.com>
 * @version 2010.1208
 * @since 0.1
 */
private class MonsterDesigner.DrawingTools : Window {
	/**
	 * Toolbox icon for the Draw editing mode.
	 */
	public RadioToolButton draw_tool;

	/**
	 * Toolbox icon for the Fill editing mode.
	 */
	public RadioToolButton fill_tool;

	/**
	 * Toolbox icon for the Insert editing mode.
	 */
	public RadioToolButton insert_tool;

	/**
	 * Toolbox icon for the Delete editing mode.
	 */
	public RadioToolButton delete_tool;

	/**
	 * Toolbox icon for the Select editing mode.
	 */
	public RadioToolButton select_tool;

	/**
	 * Toolbox icon for the Paste editing mode.
	 */
	public RadioToolButton paste_tool;

	/**
	 * Toolbox editing mode.
	 */
	private ToolBoxMode edit_mode;

	/**
	 * Constructor for the tool box window.
	 */
	public DrawingTools (Window calling_window, ToolBoxMode edit_mode) {
		set_transient_for (calling_window);
		title = "Toolbox";
		icon = calling_window.icon;
		set_default_size (108,108);
		set_type_hint (Gdk.WindowTypeHint.UTILITY);       
		this.edit_mode = edit_mode;

		var palette = new ToolPalette ();

		var group = new ToolItemGroup ("Edit Mode");
		group.header_relief = ReliefStyle.NONE;
		palette.add (group);                
        
		var draw_pixbuf = FileUtilities.load_icon ("draw");
		var draw_image = new Image.from_pixbuf (draw_pixbuf); 
		draw_tool = new RadioToolButton(null);
		draw_tool.set_icon_widget(draw_image);
		draw_tool.tooltip_markup = "Draw <b>D</b>";         
		group.insert (draw_tool, 0);

		var fill_pixbuf = FileUtilities.load_icon ("fill");
		var fill_image = new Image.from_pixbuf (fill_pixbuf); 
		fill_tool = new RadioToolButton(draw_tool.get_group());
		fill_tool.set_icon_widget(fill_image);
		fill_tool.tooltip_markup = "Fill <b>F</b>";         
		group.insert (fill_tool, 1);

		var insert_pixbuf = FileUtilities.load_icon ("insert-row");
		var insert_image = new Image.from_pixbuf (insert_pixbuf); 
		insert_tool = new RadioToolButton(draw_tool.get_group());
		insert_tool.set_icon_widget(insert_image);
		insert_tool.tooltip_markup = "Insert row <b>I</b>";         
		group.insert (insert_tool, 2);

		var delete_pixbuf = FileUtilities.load_icon ("delete-row");
		var delete_image = new Image.from_pixbuf (delete_pixbuf); 
		delete_tool = new RadioToolButton(draw_tool.get_group());
		delete_tool.set_icon_widget(delete_image);
		delete_tool.tooltip_markup = "Delete row <b>R</b>";         
		group.insert (delete_tool, 3);

		var select_pixbuf = FileUtilities.load_icon ("select");
		var select_image = new Image.from_pixbuf (select_pixbuf); 
		select_tool = new RadioToolButton(draw_tool.get_group());
		select_tool.set_icon_widget(select_image);
		select_tool.tooltip_markup = "Select <b>S</b>";         
		group.insert (select_tool, 4);

		paste_tool = new RadioToolButton.from_stock(draw_tool.get_group(), 
														STOCK_PASTE);
		paste_tool.tooltip_markup = "Paste <b>P</b>";         
		group.insert (paste_tool, 5);

		/* Connect the toolbox buttons to handlers */
		draw_tool.toggled.connect (on_draw_tool_toggled);
		fill_tool.toggled.connect (on_fill_tool_toggled);
		insert_tool.toggled.connect (on_insert_tool_toggled);
		delete_tool.toggled.connect (on_delete_tool_toggled);
		select_tool.toggled.connect (on_select_tool_toggled);
		paste_tool.toggled.connect (on_paste_tool_toggled);

		add (palette);

		edit_mode.changed.connect (on_edit_mode_changed);
	}

	/**
	 * Signal handler called when edit mode changed.
 	 */
	private void on_edit_mode_changed () {
		switch (edit_mode.mode) {
			case Mode.DRAW:
				if (!draw_tool.active) {
					draw_tool.active = true;
				}
				break;
			case Mode.FILL:
				if (!fill_tool.active) {
					fill_tool.active = true;
				}
				break;
			case Mode.INSERT_ROW:
				if (!insert_tool.active) {
					insert_tool.active = true;
				}
				break;
			case Mode.DELETE_ROW:
				if (!delete_tool.active) {
					delete_tool.active = true;
				}
				break;
			case Mode.SELECT:
				if (!select_tool.active) {
					select_tool.active = true;
				} 
				break;
			case Mode.PASTE:
				if (!paste_tool.active)	{
					paste_tool.active = true;
				} 
				break;
		}
	}

	/**
	 * Event called when the Draw radio tool button is activated.
	 */
	private void on_draw_tool_toggled () {
		if (draw_tool.active) {
			edit_mode.mode = Mode.DRAW;
		}
	}

	/**
	 * Event called when the Fill radio tool button is activated.
	 */
	private void on_fill_tool_toggled () {
		if (fill_tool.active) {
			edit_mode.mode = Mode.FILL;
		}
	}

	/**
	 * Event called when the Insert Row radio tool button is activated.
	 */
	private void on_insert_tool_toggled () {
		if (insert_tool.active) {
			edit_mode.mode = Mode.INSERT_ROW;
		}
	}

	/**
	 * Event called when the Delete Row radio tool button is activated.
	 */
	private void on_delete_tool_toggled () {
		if (delete_tool.active) {
			edit_mode.mode = Mode.DELETE_ROW;
		}
	}

	/**
	 * Event called when the Select radio tool button is activated.
	 */
	private void on_select_tool_toggled () {
		if (select_tool.active) {
			edit_mode.mode = Mode.SELECT;
		}
	}

	/**
	 * Event called when the Paste radio tool button is activated.
	 */
	private void on_paste_tool_toggled () {
		if (paste_tool.active) {
			edit_mode.mode = Mode.PASTE;
		}
	}
}
